﻿using ComponentBuilder;

using Microsoft.AspNetCore.Components;

namespace BlamanticUI.Components.Forms;
/// <summary>
/// Represents a &lt;input type="checkbox" /> form control.
/// </summary>
[HtmlTag("div")]
public class CheckBox : BlazorInputComponentBase<bool>, IHasUIComponent, IHasFitted, IHasOnDisabled,IHasInverted,IHasChildContent
{
    /// <summary>
    /// Gets or sets the display style.
    /// </summary>
    [Parameter] [CssClass] public CheckBoxStyle? Style { get; set; }
    /// <summary>
    /// Gets or sets a value indicating whether the text remove paddings.
    /// </summary>
    /// <value>
    ///   <c>true</c> if fitted; otherwise, <c>false</c>.
    /// </value>
    [Parameter] public bool Fitted { get; set; }
    /// <summary>
    /// Gets or sets a value indicating whether this is disabled.
    /// </summary>
    /// <value>
    ///   <c>true</c> if disabled; otherwise, <c>false</c>.
    /// </value>
    [Parameter][CssClass(Disabled =true)] public bool Disabled { get; set; }
    /// <summary>
    /// Gets or sets a callback method to invoke after <see cref="Disabled" /> changed.
    /// </summary>
    [Parameter] public EventCallback<bool> OnDisabled { get; set; }
    /// <summary>
    /// Gets or sets a value indicating whether is read-only
    /// </summary>
    [Parameter] [CssClass("read-only")] public bool ReadOnly { get; set; }


    /// <summary>
    /// Gets or sets a value indicating whether adapted inverted background by parent component.
    /// </summary>
    [Parameter]public bool Inverted { get; set; }

    /// <summary>
    /// Let input control to be right of label.
    /// </summary>
    [Parameter][CssClass("right aligned")] public bool RightAlign { get; set; }
    /// <summary>
    /// The segment of UI content to display. <c>null</c> to use <see cref="BlazorInputBase{TValue}.DisplayName"/> for label.
    /// </summary>
    [Parameter]public RenderFragment? ChildContent { get; set; }

    protected override void AddContent(RenderTreeBuilder builder, int sequence)
    {
        builder.CreateElement(0, "input", attributes: new
        {
            type = "checkbox",
            id = FieldIdentifier.FieldName,
            name= FieldIdentifier.FieldName,
            Disabled,
            tagindex=0,
            @class=HtmlHelper.CreateCssBuilder().Append("hidden"),
            @checked = BindConverter.FormatValue(CurrentValue),
            @readonly = ReadOnly,
            onchange = EventCallback.Factory.CreateBinder<bool>(this, __value => CurrentValue = __value, CurrentValue)
        });
        if(ChildContent is null)
        {
            ChildContent = content => content.AddContent(0, DisplayName);
        }
        builder.CreateElement(1, "label", ChildContent, new { style = "cursor:pointer", @for = FieldIdentifier.FieldName });
    }

    protected override void BuildCssClass(ICssClassBuilder builder)
    {
        builder.Append("checked", CurrentValue).Append("checkbox");
    }
}


/// <summary>
/// Defines the style of checkbox.
/// </summary>
public enum CheckBoxStyle
{
    /// <summary>
    /// The switch type.
    /// </summary>
    [CssClass("toggle")] Switch,
    /// <summary>
    /// The slider type.
    /// </summary>
    [CssClass("slider")] Slider
}